/* xxcstlbrbdnvar.i - Labor Burden variance calculate                             **/      
/* {1} entity {2} site {3} year {4} period                                        **/ 
/* Step 1 Get GCM setting for Labor Burden variance account begins character      **/
/* step 1 GCM field: xxcstallocation                                             **/
/*            value: labor_absorb/labor_actual/transfer                           **/
/*              cmmt: "5                                                          **/
/* step 1 GCM field: xxcstallocation                                             **/
/*            value: workcenter_costcenter                                        **/
/*              cmmt: 1101_1101                                                   **/
/* Step 2 using API to fetch Period amount                                        **/
/* Step 3 Get Work center Labor Burden absorb amount                              **/
/* step 3 when Work center labor                                                  **/
/* burden absorb is zero but GL balance not zero                                  **/
/* step 3 xxcstctrvar_lbr_rumtime is run time                                     **/
/* Step 4 Allocation allrate   = Work Center Variance / Work center absorb amount **/
/* Step 4 Allocation amount = allocation allrate * part work center absorb amount **/
/* Common expense allocation denominator is total burden cost to be allocated     **/
/* Last modified by GaoYan 07/25/li_year                                          **/

/*** GCM setting ***/                          
/***
mgcodemt.p             36.2.13 Generalized Codes Maintenance          08/22/19
+----------------------------- Generalized Codes ------------------------------+
|              Field Name: xxcstallocation                                     |
|                   Value: commonlist                                          |
|                                                                              |
|                Comments: com,zpc,zsc                                         |
|                                                                              |
|                   Group: APP                                                 |
+------------------------------------------------------------------------------+

mgcodemt.p             36.2.13 Generalized Codes Maintenance          08/22/19
+----------------------------- Generalized Codes ------------------------------+
|              Field Name: xxcstallocation                                     |
|                   Value: burden_absorb                                       |
|                                                                              |
|                Comments: 50010016                                            |
|                                                                              |
|                   Group: APP                                                 |
+------------------------------------------------------------------------------+

mgcodemt.p             36.2.13 Generalized Codes Maintenance          08/22/19
+----------------------------- Generalized Codes ------------------------------+
|              Field Name: xxcstallocation                                     |
|                   Value: costcenterallc-1-com                                |
|                                                                              |
|                Comments: zpc,zsc,pt                                          |
|                                                                              |
|                   Group: APP                                                 |
+------------------------------------------------------------------------------+

***/

using com.qad.eefin.buser.EntitySecurityCheck.


{us/mf/mfdtitle.i}

define variable l_error                   as logic                          no-undo.
define variable tot_glallamt              as decimal                        no-undo.
define variable allrate                   as decimal                        no-undo.
define variable tot_runtime               as decimal                        no-undo.
define variable act_or_abs                as character                      no-undo.
define variable tot_comlbramt             as decimal                        no-undo.
define variable tot_comlbramt1            as decimal                        no-undo.
define variable tot_comlbramt2            as decimal                        no-undo.
define variable tot_comlbramt3            as decimal                        no-undo.
define variable tot_combdnamt             as decimal                        no-undo.
define variable tot_combdnamt1            as decimal                        no-undo.
define variable tot_combdnamt2            as decimal                        no-undo.
define variable tot_combdnamt3            as decimal                        no-undo.
define variable tot_comovhamt             as decimal                        no-undo.
define variable tot_comovhamt1            as decimal                        no-undo.
define variable tot_comovhamt2            as decimal                        no-undo.

DEFINE VARIABLE entity              LIKE  en_entity   FORMAT "x(8)"     NO-UNDO.
DEFINE VARIABLE site                LIKE  si_site                       NO-UNDO.
DEFINE VARIABLE li_year             LIKE  xxcstctrl_ctrl.xxcstctrl_year                NO-UNDO.
DEFINE VARIABLE li_period           LIKE  xxcstctrl_ctrl.xxcstctrl_per                 NO-UNDO.
DEFINE VARIABLE li_cur_yr           LIKE  glc_year                      NO-UNDO.
DEFINE VARIABLE li_cur_per          LIKE  glc_per                       NO-UNDO.
DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
DEFINE VARIABLE lc_tmpstr           AS    CHARACTER                     NO-UNDO.
DEFINE VARIABLE li_success          AS    LOGICAL                       NO-UNDO.
DEFINE VARIABLE lc_tmpyn            AS    LOGICAL                       NO-UNDO.

/* For calling QAD Financials Data Object to check GL Entity Security */
DEFINE VARIABLE hQADFinancialsLib   AS    HANDLE                        NO-UNDO.
DEFINE VARIABLE EntitySecurity      AS    EntitySecurityCheck           NO-UNDO.
DEFINE VARIABLE valid_entity        LIKE  mfc_logical                   NO-UNDO.
DEFINE VARIABLE BufErrorMessage     AS    CHARACTER                     NO-UNDO.
DEFINE VARIABLE ErrorMessage        AS    CHARACTER                     NO-UNDO.
/* build a custom table to record */
define temp-table glandwocompare no-undo
      field woctr       like op_wkctr
	  field woruntime   as decimal
	  field wolbramt    as decimal
	  field wobdnamt    as decimal
	  field woovhamt    as decimal
	  field glcstctr    like costcentre.costcentrecode
	  field glcstctrtype like costcentre.Customshort0
	  field glcomamt       as decimal
	  field glactlbramt    as decimal
	  field glactbdnamt    as decimal
	  field glactovhamt    as decimal
	  field glabslbramt    as decimal
	  field glabsbdnamt    as decimal
	  field glabsovhamt    as decimal	  
	  field alloclogic  as character
	  
	  index inwoctr woctr
	  index inglctr glcstctr.
                                                        
define buffer b_codemstr               for code_mstr  .
define buffer b_codemstr1              for code_mstr.
define buffer b_xxcstglctramt_det      for xxcstglctramt_det.

{proxy/bgl/apitrialbalancecalculationdef.i}



/* Frame Definition */
FORM
    entity     COLON 20 
    site       COLON 20 
    li_year    COLON 20 
    li_period  COLON 20 
    WITH FRAME a WIDTH 80 SIDE-LABELS .
    setFrameLabels(FRAME a:HANDLE).
	
mainloop:
REPEAT TRANSACTION ON ERROR UNDO mainloop, RETRY mainloop:
   
   ASSIGN entity           = ""
          site             = ""
          li_year          = YEAR(TODAY)
          li_period        = MONTH(TODAY)	
		  .
   FIND FIRST glc_cal NO-LOCK WHERE glc_domain = global_domain
                                AND glc_start <= TODAY
                                AND glc_end   >= TODAY NO-ERROR.
   IF AVAILABLE glc_cal THEN 
      ASSIGN li_cur_yr  = glc_year 
             li_cur_per = glc_per
             li_year    = glc_year
             li_period  = glc_per.
   
   CLEAR FRAME a NO-PAUSE.
  /******************* Input validation *************************/ 
  /******************* Entity Site      *************************/ 
   UPDATE entity   
          WITH FRAME a EDITING:
          
      {us/mf/mfnp05.i en_mstr en_entity "en_domain = global_domain" 
                      en_entity "input entity "}
      IF recno <> ? THEN DO:
         ASSIGN entity = en_entity.
         DISPLAY
            entity
         WITH FRAME a.
      END.         
   END. /* UPDATE EDITING entity */
   
   
   IF NOT CAN-FIND( FIRST en_mstr WHERE en_domain = global_domain 
                                   AND en_entity = entity)
   THEN DO:
      /* Invalid entity */
      {us/bbi/pxmsg.i &MSGNUM=3061 &ERRORLEVEL=3}
      NEXT-PROMPT entity WITH FRAME a.
      UNDO, RETRY.
   END. /* IF NOT CAN-FIND(FIRST */
      
   /* CHECK ENTITY SECURITY */
   
   IF entity <> "" THEN DO:    
      {us/gl/glenchk.i &entity=entity &entity_ok=valid_entity} 
      IF NOT valid_entity THEN DO:
         NEXT-PROMPT entity WITH FRAME a.
         UNDO, RETRY.
      END.     
   END. /* IF entity <> "" */
  
   siloop:
   DO ON ERROR UNDO siloop, RETRY siloop:
      
      UPDATE site     
             WITH FRAME a EDITING:
         
         {us/mf/mfnp05.i si_mstr si_site "si_domain = global_domain" 
                            si_site "input site"}
            IF recno <> ? THEN DO:
               ASSIGN site = si_site.
               DISPLAY site WITH FRAME a.
            END.            
      END. /* UPDATE EDITING site */
      
      IF NOT CAN-FIND( FIRST si_mstr WHERE si_domain = global_domain 
                                      AND si_site   = site
									  AND si_entity = entity)  /* by Gaoyan site must under entity */
									  
      THEN DO:
         /* Invalid Site */
         {us/bbi/pxmsg.i &msgnum=2797 &errorlevel=3 }
         NEXT-PROMPT site WITH FRAME a.
         UNDO siloop, RETRY siloop.
      END.

      /* Site Security Check */
      {us/bbi/gprun.i ""gpsiver.p"" "(INPUT site,?,OUTPUT li_access)"}
      IF li_access = 0 THEN DO:
         /* User does not have access to this site */
         {us/bbi/pxmsg.i &MSGNUM=725 &ERRORLEVEL=3}
         NEXT-PROMPT site.
         UNDO siloop, RETRY siloop.
      END. /* IF li_access = 0 */      
   END. /* siloop */
   
   yrloop:
   
   DO ON ERROR UNDO yrloop, RETRY yrloop:
   
      UPDATE li_year  
             li_period WITH FRAME a.
        /*     
      IF LENGTH(li_year) <> 4 THEN DO:
         {pxmsg.i &msgnum=3900 &errorlevel=1 &msgarg1=""Year"" }
         NEXT-PROMPT li_year.
         UNDO yrloop, RETRY yrloop.
      END.
            
      IF li_period < 1 OR li_period > 12 THEN DO:
         {pxmsg.i &msgnum=282 &errorlevel=1 &msgarg1=""Period"" }
         NEXT-PROMPT li_period.
         UNDO yrloop, RETRY yrloop.
      END.
      */
   END. /* yrloop */
   
   
    IF CAN-FIND( FIRST xxcstctrl_ctrl NO-lOCK 
      WHERE xxcstctrl_ctrl.xxcstctrl_domain = global_domain
        AND xxcstctrl_ctrl.xxcstctrl_entity = entity
        AND xxcstctrl_ctrl.xxcstctrl_site   = site
		/*
        AND xxcstctrl_ctrl.xxcstctrl_ID     = "CurYrper"
		*/
        AND xxcstctrl_ctrl.xxcstctrl_year   = li_year
        AND xxcstctrl_ctrl.xxcstctrl_per    = li_period) THEN DO: 
		

      /* Do you want to delete date from input Year and Period until current 
         year and Period */
		 
      ASSIGN lc_tmpstr = getTermLabel("XXHISTDUP_DEL",37) + " " + 
                         STRING(li_year) + "/" + STRING(li_period) + " " +
                         LC(getTermLabel("UNTIL",6)) + " " + 
                         LC(getTermLabel("CURRENT",7)) + " " +
                         STRING(li_cur_yr) + "/" + STRING(li_cur_per) + " ?"
             lc_tmpyn   = NO
             .
      
      MESSAGE lc_tmpstr UPDATE lc_tmpyn.
      IF lc_tmpyn THEN DO:
          for each xxcstglctramt_det where xxcstglctramt_det.xxcstglctramt_domain            = global_domain
	                           and xxcstglctramt_det.xxcstglctramt_entity            = entity /* {} */							  
							   and xxcstglctramt_det.xxcstglctramt_year              = li_year
							   and xxcstglctramt_det.xxcstglctramt_period            = li_period
							    .
		delete xxcstglctramt_det .
      end.
      END.
      ELSE DO:
         ASSIGN li_year    = li_cur_yr
                li_period  = li_cur_per.     
      END.
      
   END. /* IF CAN-FIND ( FIRST xxcstctrl_ctrl */ 
  
/*
  message "GO-ON" view-as alert-box.
  */
  

empty temp-table tqApiTrialBalanceCalculation.
empty temp-table tFilter.


create tFilter.
assign
       tFilter.tcBusinessFieldName = "tCompany.CompanyCode"
       tFilter.tcDataType          = "c"
       tFilter.tcOperator          = "="
       tFilter.tcParameterValue    = entity /* {1} */.
 create tFilter.
     assign
       tFilter.tcBusinessFieldName = "tPeriod.PeriodYearPeriod"
       tFilter.tcDataType          = "i"
       tFilter.tcOperator          = ">="
       tFilter.tcParameterValue    = string(li_year) + string(li_period,"99") /* {3} + {4} */.           
 create tFilter.
     assign
       tFilter.tcBusinessFieldName = "tPeriod.PeriodYearPeriod"
       tFilter.tcDataType          = "i"
       tFilter.tcOperator          = "<="
       tFilter.tcParameterValue    = string(li_year) + string(li_period,"99") /* {3} + {4} */.   	   
 create tFilter.
       assign
       tFilter.tcBusinessFieldName = "tLayer.LayerTypeCode"
       tFilter.tcDataType          = "c"
	   /*
       tFilter.tcOperator          = "can-do"
	   */
	   tFilter.tcOperator          = "="
       tFilter.tcParameterValue    = "official".       
 
 create tFilter.
        assign
       tFilter.tcBusinessFieldName = "IsSumOnBaseCurrency"
       tFilter.tcDataType          = "l"
       tFilter.tcOperator          = "="
       tFilter.tcParameterValue    = "true".    
 create tFilter.
        assign
       tFilter.tcBusinessFieldName = "IsSumOnDaybook"
       tFilter.tcDataType          = "l"
       tFilter.tcOperator          = "="
       tFilter.tcParameterValue    = "true".  
/* for YFPO QAD use sub account to collect labor burden expense */
  create tFilter.
        assign
       tFilter.tcBusinessFieldName = "IsSumOnSubAccount"
       tFilter.tcDataType          = "l"
       tFilter.tcOperator          = "="
       tFilter.tcParameterValue    = "false". 
  create tFilter.
        assign
       tFilter.tcBusinessFieldName = "IsSumOnCostCenter"
       tFilter.tcDataType          = "l"
       tFilter.tcOperator          = "="
       tFilter.tcParameterValue    = "true". 
 create tFilter.
        assign
       tFilter.tcBusinessFieldName = "IsSumOnProject"
       tFilter.tcDataType          = "l"
       tFilter.tcOperator          = "="
       tFilter.tcParameterValue    = "true". 
 create tFilter.
        assign
       tFilter.tcBusinessFieldName = "IsSumOnSaf"
       tFilter.tcDataType          = "l"
       tFilter.tcOperator          = "="
       tFilter.tcParameterValue    = "true". 
	   
/* For YFPO the variance is calculated by user for each sub account */
for each code_mstr where code_mstr.code_domain = global_domain 
                    and  code_mstr.code_fldname  = "xxcstallocation" 
                    and (code_mstr.code_value begins "burden"
                    or   code_mstr.code_value begins "labor"
					or   code_mstr.code_value begins "overhead")
                    no-lock.
	
create tFilter.
        assign
        tFilter.tcBusinessFieldName = "tGL.GLCode"
        tFilter.tcDataType          = "c"
        tFilter.tcOperator          = "begins"
        tFilter.tcParameterValue    = trim(code_mstr.code_cmmt).
end.

{proxy/bgl/apitrialbalancecalculationrun.i}

   /* scenario 1: Work center have Actual amount and no runtime will be allocated to other costcentre*/
   /* scenario 2: Work center have runtime amount and no actual amount 
       maybe costcentre mapping is wrong or no need to allocatie or allocation other costcentre amount(current logic)*/
   /* scenario 3: Work center have Actual amount and type is not "GG" and have no runtime means GCM setting is wrong*/
   /* scenario 1: Work center have Actual amount and no runtime */
   /* from dataset to assign labor/Burden actual and absorb amount value */

   for each tqApiTrialBalanceCalculation where 
            tqApiTrialBalanceCalculation.tdPeriodDebitLc  <> 0 or
			tqApiTrialBalanceCalculation.tdPeriodCreditLc <> 0 	:
			
	  /* Get Costcentre type */			
      find first domains where domains.domaincode = global_domain no-lock no-error.
      for each domainsharedset of domains no-lock .
      find SharedSet of domainsharedset  no-lock no-error.
      if sharedset.sharedsettypecode = "Costcentre" then
      do:
          find Costcentre where Costcentre.SharedSet_ID   = SharedSet.SharedSet_ID 
                            and Costcentre.costcentrecode = tqApiTrialBalanceCalculation.tcCostCentreCode 
                            no-lock no-error.
      end.
      end. /* for each domainsharedset of domains no-lock . */
		
      find xxcstglctramt_det where xxcstglctramt_det.xxcstglctramt_domain            = global_domain
	                           and xxcstglctramt_det.xxcstglctramt_entity            = entity /* {} */
							   and xxcstglctramt_det.xxcstglctramt_year              = li_year
							   and xxcstglctramt_det.xxcstglctramt_period            = li_period
							   and xxcstglctramt_det.xxcstglctramt_glcode            = tqApiTrialBalanceCalculation.tcGLCode
							   and xxcstglctramt_det.xxcstglctramt_costcentercode    = tqApiTrialBalanceCalculation.tcCostCentreCode
							   no-error.
	  find code_mstr where  code_mstr.code_domain  = global_domain 
	                   and  code_mstr.code_fldname = "xxcstallocation" 
					   and 	tqApiTrialBalanceCalculation.tcGLCode begins code_mstr.code_cmmt    
	                   no-lock no-error.
     

   if not available xxcstglctramt_det then
      do:	  
	  create xxcstglctramt_det.
	  
	  assign xxcstglctramt_det.xxcstglctramt_domain          = global_domain
	         xxcstglctramt_det.xxcstglctramt_entity          = tcCompanyCode
	         xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	         xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */
	         xxcstglctramt_det.xxcstglctramt_glcode          = tcGLCode
	         xxcstglctramt_det.xxcstglctramt_costcentercode  = tcCostCentreCode
	         xxcstglctramt_det.xxcstglctramt_costcentertype  = Costcentre.Customshort0 
	         xxcstglctramt_det.xxcstglctramt_glaccounttype   = code_mstr.code_value when available code_mstr
	         .
	  end. 
	/*
	  message tqApiTrialBalanceCalculation.tdPeriodDebitLc tqApiTrialBalanceCalculation.tdPeriodCreditLc view-as alert-box.
	 */
	 assign
             xxcstglctramt_det.xxcstglctramt_dr_amt          = xxcstglctramt_det.xxcstglctramt_dr_amt + 
			                                                   tqApiTrialBalanceCalculation.tdPeriodDebitLc
             xxcstglctramt_det.xxcstglctramt_cr_amt          = xxcstglctramt_det.xxcstglctramt_cr_amt +        
			                                                   tqApiTrialBalanceCalculation.tdPeriodCreditLc
			 
             xxcstglctramt_det.xxcstglctramt_lbr_glactamt    = xxcstglctramt_det.xxcstglctramt_lbr_glactamt +
			                                                   (tqApiTrialBalanceCalculation.tdPeriodDebitLc - tqApiTrialBalanceCalculation.tdPeriodCreditLc)
			                                                   when (available code_mstr and code_value = "labor_actual")            
			 xxcstglctramt_det.xxcstglctramt_lbr_glabsamt    = xxcstglctramt_det.xxcstglctramt_lbr_glabsamt -
			                                                   (tqApiTrialBalanceCalculation.tdPeriodDebitLc - tqApiTrialBalanceCalculation.tdPeriodCreditLc)
			                                                   when (available code_mstr and code_value = "labor_absorb")
             xxcstglctramt_det.xxcstglctramt_bdn_glactamt    = xxcstglctramt_det.xxcstglctramt_bdn_glactamt +
			                                                   (tqApiTrialBalanceCalculation.tdPeriodDebitLc - tqApiTrialBalanceCalculation.tdPeriodCreditLc)
			                                                   when (available code_mstr and code_value = "burden_actual")
             xxcstglctramt_det.xxcstglctramt_bdn_glabsamt    = xxcstglctramt_det.xxcstglctramt_bdn_glabsamt -
			                                                   (tqApiTrialBalanceCalculation.tdPeriodDebitLc - tqApiTrialBalanceCalculation.tdPeriodCreditLc)
			                                                   when (available code_mstr and code_value = "burden_absorb")
             xxcstglctramt_det.xxcstglctramt_ovh_glactamt    = xxcstglctramt_det.xxcstglctramt_ovh_glactamt +
			                                                   (tqApiTrialBalanceCalculation.tdPeriodDebitLc - tqApiTrialBalanceCalculation.tdPeriodCreditLc)
			                                                   when (available code_mstr and code_value = "overhead_actual")
             xxcstglctramt_det.xxcstglctramt_ovh_glabsamt    = xxcstglctramt_det.xxcstglctramt_ovh_glabsamt -
			                                                   (tqApiTrialBalanceCalculation.tdPeriodDebitLc - tqApiTrialBalanceCalculation.tdPeriodCreditLc)
			                                                   when (available code_mstr and code_value = "overhead_absorb")
															   .
	  
 

    end. /* for each tqApiTrialBalanceCalculation */
	/** summarized total actual and absorb burden/labor amount by costcenter type **/
	for each xxcstglctramt_det where xxcstglctramt_det.xxcstglctramt_domain = global_domain
	                             and xxcstglctramt_det.xxcstglctramt_entity = entity
								 and xxcstglctramt_det.xxcstglctramt_year   = li_year
								 and xxcstglctramt_det.xxcstglctramt_period = li_period
								 .
								 
      for each b_xxcstglctramt_det where b_xxcstglctramt_det.xxcstglctramt_domain          = global_domain
	                                 and b_xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                 and b_xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                 and b_xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */
	                                 and b_xxcstglctramt_det.xxcstglctramt_costcentertype  = xxcstglctramt_det.xxcstglctramt_costcentertype	
									 and b_xxcstglctramt_det.xxcstglctramt_glaccounttype   = xxcstglctramt_det.xxcstglctramt_glaccounttype.
			
          assign
		    
  		     b_xxcstglctramt_det.xxcstglctramt_lbr_totglactamt    = b_xxcstglctramt_det.xxcstglctramt_lbr_totglactamt +
			                                                         xxcstglctramt_det.xxcstglctramt_lbr_glactamt  
																	when 	xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_actual"                                                            
			 b_xxcstglctramt_det.xxcstglctramt_lbr_totglabsamt    = b_xxcstglctramt_det.xxcstglctramt_lbr_totglabsamt +
			                                                        xxcstglctramt_det.xxcstglctramt_lbr_glabsamt 
																	when   xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb"
             b_xxcstglctramt_det.xxcstglctramt_bdn_totglactamt    = xxcstglctramt_det.xxcstglctramt_bdn_glactamt + 
			                                                        b_xxcstglctramt_det.xxcstglctramt_bdn_totglactamt     
																	when xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_actual"
             b_xxcstglctramt_det.xxcstglctramt_bdn_totglabsamt    = b_xxcstglctramt_det.xxcstglctramt_bdn_totglabsamt + 
			                                                        xxcstglctramt_det.xxcstglctramt_bdn_glabsamt   
																	when xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb"
             b_xxcstglctramt_det.xxcstglctramt_ovh_totglactamt    = xxcstglctramt_det.xxcstglctramt_ovh_glactamt + 
			                                                        b_xxcstglctramt_det.xxcstglctramt_ovh_totglactamt     
																	when xxcstglctramt_det.xxcstglctramt_glaccounttype = "overhead_actual"
             b_xxcstglctramt_det.xxcstglctramt_ovh_totglabsamt    = b_xxcstglctramt_det.xxcstglctramt_ovh_totglabsamt + 
			                                                        xxcstglctramt_det.xxcstglctramt_ovh_glabsamt   
																	when xxcstglctramt_det.xxcstglctramt_glaccounttype = "overhead_absorb".

																	/*	                                                   
	  message b_xxcstglctramt_det.xxcstglctramt_lbr_totglabsamt
	          xxcstglctramt_det.xxcstglctramt_lbr_glabsamt 
	          xxcstglctramt_det.xxcstglctramt_costcentercode 
			  xxcstglctramt_det.xxcstglctramt_glcode view-as alert-box.
       */ 
    end.	 
	end.
	
    /**** 1st  Common expense allocate to work center common costcentre base on total each absorb costcentre amount ****/
	/****      1.1  Summarized Common expense amount                                  ****/
    /****      1.2  Summarized each absorb labor burden amount by costcentre type     ****/
    /****      1.3  Rate = Total common expense labor(burden) / Total each costcentre type absorb labor(burden) expense   ****/	
	/****      1.4  allocation amount = Rate * each Total each costcentre type absorb labor(burden) ****/
	/****                                                                                      ****/
	/**** 2nd  if costcentre have actual expense amount but have no absorb expense amount      ****/
	/****      then the expense will be treated as work center common expense ***/
	/****      2.1  compare actual expense and absorb expense costcentre  and
                 	summarized total amount by costcentre type for the costcentre have no absorb amount    ****/
	/**** 3rd  work center common costcentre expense allocate to each work center  ****/
	/****       3.1  rate = 1.4 allocation amount + 2.1 / each Total each costcentre absorb labor(burden) ****/
	/****       3.2   allocation amount = Rate * each Total each costcentre absorb labor(burden) ****/
    tot_combdnamt  = 0.
    tot_combdnamt1 = 0.
    tot_combdnamt2 = 0.
    tot_comlbramt  = 0.
    tot_comlbramt1 = 0.
    tot_comlbramt2 = 0.
	tot_comovhamt  = 0.
	/* summarized total absorb labor and burden amount */		
    for each xxcstglctramt_det  where  xxcstglctramt_det.xxcstglctramt_domain          = global_domain
	                                      and xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                      and xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                      and xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */
										  .

      if xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb" then 
	  tot_comlbramt = tot_comlbramt + xxcstglctramt_det.xxcstglctramt_lbr_glabsamt.
      if xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb" then 
	  tot_combdnamt = tot_combdnamt + xxcstglctramt_det.xxcstglctramt_bdn_glabsamt.
      if xxcstglctramt_det.xxcstglctramt_glaccounttype = "overhead_absorb" then 
	  tot_comovhamt = tot_comovhamt + xxcstglctramt_det.xxcstglctramt_ovh_glabsamt.	  
	  status default string(xxcstglctramt_det.xxcstglctramt_lbr_glactamt) + " A"	.
	  
    end.
    /* absorb and actual activity compare */
	/* if there havaeactual amount but not absorb amount for some costcenter type  the program will miss it 
	 so the missed costcenter amount will be treated as common amount */
    for each xxcstglctramt_det  where  xxcstglctramt_det.xxcstglctramt_domain           = global_domain
	                                and xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                and xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                and xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */ 
								    break by xxcstglctramt_det.xxcstglctramt_glaccounttype
										  by xxcstglctramt_det.xxcstglctramt_costcentertype
										  by xxcstglctramt_det.xxcstglctramt_costcentercode.
										  
		if xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_actual" 
		
		then do:		   
	    for first b_xxcstglctramt_det where b_xxcstglctramt_det.xxcstglctramt_domain    = global_domain
	                                      and b_xxcstglctramt_det.xxcstglctramt_entity    = xxcstglctramt_det.xxcstglctramt_entity
	                                      and b_xxcstglctramt_det.xxcstglctramt_year      = xxcstglctramt_det.xxcstglctramt_year
	                                      and b_xxcstglctramt_det.xxcstglctramt_period    = xxcstglctramt_det.xxcstglctramt_period 
										  and b_xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb"
										  and b_xxcstglctramt_det.xxcstglctramt_costcentercode = xxcstglctramt_det.xxcstglctramt_costcentercode
										  .
		end.
		if available  b_xxcstglctramt_det then 
		assign
		b_xxcstglctramt_det.xxcstglctramt_lbr_glactamt = b_xxcstglctramt_det.xxcstglctramt_lbr_glactamt + 
		                                                 xxcstglctramt_det.xxcstglctramt_lbr_glactamt.
		if not available b_xxcstglctramt_det then do:
		   
		   for each b_xxcstglctramt_det where b_xxcstglctramt_det.xxcstglctramt_domain    = global_domain
	                                      and b_xxcstglctramt_det.xxcstglctramt_entity    = xxcstglctramt_det.xxcstglctramt_entity
	                                      and b_xxcstglctramt_det.xxcstglctramt_year      = xxcstglctramt_det.xxcstglctramt_year
	                                      and b_xxcstglctramt_det.xxcstglctramt_period    = xxcstglctramt_det.xxcstglctramt_period 
										  and b_xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb"
										  and b_xxcstglctramt_det.xxcstglctramt_costcentertype = xxcstglctramt_det.xxcstglctramt_costcentertype
										  .
           assign
			b_xxcstglctramt_det.xxcstglctramt_lbr_allcom1amt =   b_xxcstglctramt_det.xxcstglctramt_lbr_allcom1amt 
			                                                     + xxcstglctramt_det.xxcstglctramt_lbr_glactamt.		
	
		end.
		end.
		end.
		if xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_actual" 
		
		then do:		   
		   for first b_xxcstglctramt_det where b_xxcstglctramt_det.xxcstglctramt_domain   = global_domain
	                                      and b_xxcstglctramt_det.xxcstglctramt_entity    = xxcstglctramt_det.xxcstglctramt_entity
	                                      and b_xxcstglctramt_det.xxcstglctramt_year      = xxcstglctramt_det.xxcstglctramt_year
	                                      and b_xxcstglctramt_det.xxcstglctramt_period    = xxcstglctramt_det.xxcstglctramt_period 
										  and b_xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb"
										  and b_xxcstglctramt_det.xxcstglctramt_costcentercode = xxcstglctramt_det.xxcstglctramt_costcentercode
										  .
		   end.
		   if available b_xxcstglctramt_det then do:
			b_xxcstglctramt_det.xxcstglctramt_bdn_glactamt = b_xxcstglctramt_det.xxcstglctramt_bdn_glactamt 
			                                                     + xxcstglctramt_det.xxcstglctramt_bdn_glactamt.		
          end. 
		   if not available b_xxcstglctramt_det then do:
		   for each b_xxcstglctramt_det where b_xxcstglctramt_det.xxcstglctramt_domain    = global_domain
	                                      and b_xxcstglctramt_det.xxcstglctramt_entity    = xxcstglctramt_det.xxcstglctramt_entity
	                                      and b_xxcstglctramt_det.xxcstglctramt_year      = xxcstglctramt_det.xxcstglctramt_year
	                                      and b_xxcstglctramt_det.xxcstglctramt_period    = xxcstglctramt_det.xxcstglctramt_period 
										  and b_xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb"
										  and b_xxcstglctramt_det.xxcstglctramt_costcentertype = xxcstglctramt_det.xxcstglctramt_costcentertype
										  .
           assign
			b_xxcstglctramt_det.xxcstglctramt_bdn_allcom1amt = b_xxcstglctramt_det.xxcstglctramt_bdn_allcom1amt 
			                                                     + xxcstglctramt_det.xxcstglctramt_bdn_glactamt.
		  end.
          end. 
           			
		end.
    end. 		
    /* mark common expense for missed costcenter type */
	    for each xxcstglctramt_det  where  xxcstglctramt_det.xxcstglctramt_domain          = global_domain
	                                      and xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                      and xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                      and xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */
										 
										  .
	   find code_mstr where code_mstr.code_domain = global_domain
 	                        and code_mstr.code_fldname = "xxcstallocation"	
                            and code_mstr.code_value   = "commonlist"
							and lookup(xxcstglctramt_det.xxcstglctramt_costcentertype,code_mstr.code_cmmt) > 0
							no-lock no-error .
		if not available code_mstr then do:
		/*
					message xxcstglctramt_det.xxcstglctramt_costcentertype view-as alert-box.
		*/
		if xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_actual" then do:
		 find first b_xxcstglctramt_det where b_xxcstglctramt_det.xxcstglctramt_domain          = global_domain
	                                      and b_xxcstglctramt_det.xxcstglctramt_entity          = xxcstglctramt_det.xxcstglctramt_entity
	                                      and b_xxcstglctramt_det.xxcstglctramt_year            = xxcstglctramt_det.xxcstglctramt_year      /* {3} */
	                                      and b_xxcstglctramt_det.xxcstglctramt_period          = xxcstglctramt_det.xxcstglctramt_period    /* {4} */						  
		                                  and b_xxcstglctramt_det.xxcstglctramt_costcentertype  = xxcstglctramt_det.xxcstglctramt_costcentertype
		                                  and b_xxcstglctramt_det.xxcstglctramt_glaccounttype   = "labor_absorb"
		 							      no-error.
		    if not available b_xxcstglctramt_det then do:	
			assign xxcstglctramt_det.xxcstglctramt_rmk = "COM".
            end.
		  end. /* if xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_actual" then do: */
		if xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_actual" then do:
		 find first b_xxcstglctramt_det where b_xxcstglctramt_det.xxcstglctramt_domain          = global_domain
	                                      and b_xxcstglctramt_det.xxcstglctramt_entity          = xxcstglctramt_det.xxcstglctramt_entity
	                                      and b_xxcstglctramt_det.xxcstglctramt_year            = xxcstglctramt_det.xxcstglctramt_year      /* {3} */
	                                      and b_xxcstglctramt_det.xxcstglctramt_period          = xxcstglctramt_det.xxcstglctramt_period    /* {4} */						  
		                                  and b_xxcstglctramt_det.xxcstglctramt_costcentertype  = xxcstglctramt_det.xxcstglctramt_costcentertype
										  and b_xxcstglctramt_det.xxcstglctramt_glaccounttype   = "burden_absorb"
										 
										  no-error.
		    if not available b_xxcstglctramt_det then do:	
			assign xxcstglctramt_det.xxcstglctramt_rmk = "COM".
            end.
		  end. /* if xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_actual" then do: */		  
		end. /* if not available code_mstr */
		end. /* for each xxcstglctramt_det */
	/* Common expense rate calculate */
      for each xxcstglctramt_det  where  xxcstglctramt_det.xxcstglctramt_domain          = global_domain
	                                      and xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                      and xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                      and xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */
                                          .
	   status default string(xxcstglctramt_det.xxcstglctramt_lbr_glactamt)	.								
	   find first code_mstr where code_mstr.code_domain = global_domain
 	                    and code_mstr.code_fldname = "xxcstallocation"		
                        and entry(1,code_mstr.code_value,"-") =  "costcenterallc"	
                        and entry(2,code_mstr.code_value,"-") =  "1"
                        no-lock no-error.						
       if  (xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_actual" 
		and xxcstglctramt_det.xxcstglctramt_costcentertype = entry(3,code_mstr.code_value,"-") )
		or (xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_actual" and
		   xxcstglctramt_det.xxcstglctramt_rmk = "COM")
		then 						
        assign
		xxcstglctramt_det.xxcstglctramt_lbrrate = if xxcstglctramt_det.xxcstglctramt_lbr_totglactamt / tot_comlbramt = ?
		                                          then 0 
												  else 
												  xxcstglctramt_det.xxcstglctramt_lbr_totglactamt / tot_comlbramt.
		
	   if  (xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_actual" 
		and xxcstglctramt_det.xxcstglctramt_costcentertype = entry(3,code_mstr.code_value,"-"))
		or (xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_actual" and
		   xxcstglctramt_det.xxcstglctramt_rmk = "COM")		then 		
        assign		
        xxcstglctramt_det.xxcstglctramt_bdnrate = if xxcstglctramt_det.xxcstglctramt_bdn_totglactamt / tot_combdnamt = ?
		                                          then 0
												  else 
												  xxcstglctramt_det.xxcstglctramt_bdn_totglactamt / tot_combdnamt.

    end.
	/* calculate common expense allocation amount and assign it to absorb record */
	
	find first code_mstr where code_mstr.code_domain = global_domain
	                       and code_mstr.code_fldname = "xxcstallocation"		
                           and entry(1,code_mstr.code_value,"-") =  "costcenterallc"	
                           and entry(2,code_mstr.code_value,"-") =  "1"
                           no-lock no-error.

    find first xxcstglctramt_det  where  xxcstglctramt_det.xxcstglctramt_domain           = global_domain
	                                and xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                and xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                and xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */ 
                                    and xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_actual"
									and xxcstglctramt_det.xxcstglctramt_costcentertype =  entry(3,code_mstr.code_value,"-")
									no-error.
	if 	available 	xxcstglctramt_det then do:						
         
    for each b_xxcstglctramt_det where  b_xxcstglctramt_det.xxcstglctramt_domain          = global_domain
	                                        and b_xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                        and b_xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                        and b_xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */										      
	                                        .
		/*
	     message string(b_xxcstglctramt_det.xxcstglctramt_lbr_totglabsamt) view-as alert-box.	
        */		 
		if b_xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb"  then 
		   b_xxcstglctramt_det.xxcstglctramt_lbr_allcomamt = b_xxcstglctramt_det.xxcstglctramt_lbr_allcomamt +
		                                                     b_xxcstglctramt_det.xxcstglctramt_lbr_totglabsamt
     		                                               * xxcstglctramt_det.xxcstglctramt_lbrrate .
														   
    end.
	end.

    find first xxcstglctramt_det  where  xxcstglctramt_det.xxcstglctramt_domain           = global_domain
	                                and xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                and xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                and xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */ 
                                    and xxcstglctramt_det.xxcstglctramt_glaccounttype   = "labor_actual"
									and xxcstglctramt_det.xxcstglctramt_rmk             = "COM" 
									no-error.
	if 	available xxcstglctramt_det then do:						
         
    for each b_xxcstglctramt_det where  b_xxcstglctramt_det.xxcstglctramt_domain          = global_domain
	                                        and b_xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                        and b_xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                        and b_xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */										      
	                                        .	 
		if b_xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb"  then 
		   b_xxcstglctramt_det.xxcstglctramt_lbr_allcomamt = b_xxcstglctramt_det.xxcstglctramt_lbr_allcomamt +
		                                                     b_xxcstglctramt_det.xxcstglctramt_lbr_totglabsamt
     		                                               * xxcstglctramt_det.xxcstglctramt_lbrrate .
														   
    end.
	end.	
	
    find first xxcstglctramt_det  where  xxcstglctramt_det.xxcstglctramt_domain           = global_domain
	                                and xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                and xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                and xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */ 
                                    and xxcstglctramt_det.xxcstglctramt_glaccounttype   = "burden_actual"
									and xxcstglctramt_det.xxcstglctramt_costcentertype =  entry(3,code_mstr.code_value,"-")
									no-error.
	if 	available xxcstglctramt_det then do:						
         
    for each b_xxcstglctramt_det where  b_xxcstglctramt_det.xxcstglctramt_domain          = global_domain
	                                        and b_xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                        and b_xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                        and b_xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */										      
	                                        .	 
		if b_xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb"  then 
		   b_xxcstglctramt_det.xxcstglctramt_bdn_allcomamt = b_xxcstglctramt_det.xxcstglctramt_bdn_allcomamt +
		                                                     b_xxcstglctramt_det.xxcstglctramt_bdn_totglabsamt
     		                                               * xxcstglctramt_det.xxcstglctramt_bdnrate .
														   
    end.
	end.	
	
	find first xxcstglctramt_det  where  xxcstglctramt_det.xxcstglctramt_domain           = global_domain
	                                and xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                and xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                and xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */ 
                                    and xxcstglctramt_det.xxcstglctramt_glaccounttype   = "burden_actual"
									and xxcstglctramt_det.xxcstglctramt_rmk             = "COM" 
									no-error.
	if 	available xxcstglctramt_det then do:						
         
    for each b_xxcstglctramt_det where  b_xxcstglctramt_det.xxcstglctramt_domain          = global_domain
	                                        and b_xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                        and b_xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                        and b_xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */										      
	                                        .	 
		if b_xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb"  then 
		   b_xxcstglctramt_det.xxcstglctramt_bdn_allcomamt = b_xxcstglctramt_det.xxcstglctramt_bdn_allcomamt +
		                                                     b_xxcstglctramt_det.xxcstglctramt_bdn_totglabsamt
     		                                               * xxcstglctramt_det.xxcstglctramt_bdnrate .
														   
    end.
	end.	
	

	/*** costcenter type common expense copy to each work center ***/
    for each xxcstglctramt_det  where  xxcstglctramt_det.xxcstglctramt_domain           = global_domain
	                                and xxcstglctramt_det.xxcstglctramt_entity          = entity
	                                and xxcstglctramt_det.xxcstglctramt_year            = li_year /* {3} */
	                                and xxcstglctramt_det.xxcstglctramt_period          = li_period    /* {4} */ 
								    break by xxcstglctramt_det.xxcstglctramt_glaccounttype
										  by xxcstglctramt_det.xxcstglctramt_costcentertype
										  by xxcstglctramt_det.xxcstglctramt_costcentercode.
	   
	   find first code_mstr where code_mstr.code_domain = global_domain
	                       and code_mstr.code_fldname = "xxcstallocation"		
                           and entry(1,code_mstr.code_value,"-") =  "costcenterallc"	
                           and entry(2,code_mstr.code_value,"-") =  "2"
						   and entry(3,code_mstr.code_value,"-") = xxcstglctramt_det.xxcstglctramt_costcentertype 
                           no-lock no-error.
        if available code_mstr then do:		
		if xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_actual"
		  
		then do:		   

		   for each b_xxcstglctramt_det where b_xxcstglctramt_det.xxcstglctramt_domain    = global_domain
	                                      and b_xxcstglctramt_det.xxcstglctramt_entity    = xxcstglctramt_det.xxcstglctramt_entity
	                                      and b_xxcstglctramt_det.xxcstglctramt_year      = xxcstglctramt_det.xxcstglctramt_year
	                                      and b_xxcstglctramt_det.xxcstglctramt_period    = xxcstglctramt_det.xxcstglctramt_period 
										  and b_xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb"
										  and lookup(b_xxcstglctramt_det.xxcstglctramt_costcentertype, code_mstr.code_cmmt) > 0										  .
           assign
			b_xxcstglctramt_det.xxcstglctramt_lbr_allcom1amt =   b_xxcstglctramt_det.xxcstglctramt_lbr_allcom1amt 
			                                                     + xxcstglctramt_det.xxcstglctramt_lbr_glactamt.		
	
		   end.
		end.
		
		if xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_actual"  	
		then do:		   
		  
		   for each b_xxcstglctramt_det where b_xxcstglctramt_det.xxcstglctramt_domain    = global_domain
	                                      and b_xxcstglctramt_det.xxcstglctramt_entity    = xxcstglctramt_det.xxcstglctramt_entity
	                                      and b_xxcstglctramt_det.xxcstglctramt_year      = xxcstglctramt_det.xxcstglctramt_year
	                                      and b_xxcstglctramt_det.xxcstglctramt_period    = xxcstglctramt_det.xxcstglctramt_period 
										  and b_xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb"
										  and lookup(b_xxcstglctramt_det.xxcstglctramt_costcentertype, code_mstr.code_cmmt) > 0	
										  .
           assign
			b_xxcstglctramt_det.xxcstglctramt_bdn_allcom1amt = b_xxcstglctramt_det.xxcstglctramt_bdn_allcom1amt 
			                                                     + xxcstglctramt_det.xxcstglctramt_bdn_glactamt.
		  end.           			
		end.
		
		end. /* if available code_mstr */
    end. 										  
	
	/*** common labor burden amount allocate to each work center ****/
         for each xxcstglctramt_det where     xxcstglctramt_det.xxcstglctramt_domain   = global_domain
	                                      and xxcstglctramt_det.xxcstglctramt_entity    = xxcstglctramt_det.xxcstglctramt_entity
	                                      and xxcstglctramt_det.xxcstglctramt_year      = xxcstglctramt_det.xxcstglctramt_year
	                                      and xxcstglctramt_det.xxcstglctramt_period    = xxcstglctramt_det.xxcstglctramt_period										  
										  . 
		 if xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb"  then do:
		    xxcstglctramt_det.xxcstglctramt_lbrrate1 = (xxcstglctramt_det.xxcstglctramt_lbr_allcomamt + 
		                                               xxcstglctramt_det.xxcstglctramt_lbr_allcom1amt) /
													   xxcstglctramt_det.xxcstglctramt_lbr_totglabsamt.
			xxcstglctramt_det.xxcstglctramt_lbr_allamt	= 	xxcstglctramt_det.xxcstglctramt_lbrrate1 * 
                                                           xxcstglctramt_det.xxcstglctramt_lbr_glabsamt.	
            xxcstglctramt_det.xxcstglctramt_lbr_varamt = xxcstglctramt_det.xxcstglctramt_lbr_allamt +
                                                         xxcstglctramt_det.xxcstglctramt_lbr_glactamt -
                                                         xxcstglctramt_det.xxcstglctramt_lbr_glabsamt	.													 
		 end.
		 if xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb"  then do:
		    xxcstglctramt_det.xxcstglctramt_bdnrate1 = (xxcstglctramt_det.xxcstglctramt_bdn_allcomamt + 
		                                               xxcstglctramt_det.xxcstglctramt_bdn_allcom1amt) /
													   xxcstglctramt_det.xxcstglctramt_bdn_totglabsamt.
			xxcstglctramt_det.xxcstglctramt_bdn_allamt	= 	xxcstglctramt_det.xxcstglctramt_bdnrate1 * 
                                                           xxcstglctramt_det.xxcstglctramt_bdn_glabsamt.
           xxcstglctramt_det.xxcstglctramt_bdn_varamt  = xxcstglctramt_det.xxcstglctramt_bdn_allamt +
                                                         xxcstglctramt_det.xxcstglctramt_bdn_glactamt -
                                                         xxcstglctramt_det.xxcstglctramt_bdn_glabsamt	.																   
		 end.										  
		 							  
										  
				
		end.

	/*** current labor burden variable calculate for each part number  ***/
	/*** Fetch workcenter_costcenter mapping from GCM                  ***/
	/*** if no GCM setting default workcenter_costcenter is same       ***/
	/*** summarized total labor absorb amount for each work center assign it to xxcstctrvar_lbr_varamt ***/
	/*** allocation rate = costcenter variance amount / total labor absorb amount for each work center ***/
	/*** allocation amount = each part number labor amount in work center * allocation rate   ***/
	
             for each xxcstglctramt_det where xxcstglctramt_det.xxcstglctramt_domain   = global_domain
	                                      and xxcstglctramt_det.xxcstglctramt_entity    = entity
	                                      and xxcstglctramt_det.xxcstglctramt_year      = li_year
	                                      and xxcstglctramt_det.xxcstglctramt_period    = li_period										  
										  and (xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb"
										   or xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb") 
										   no-lock .
			    find code_mstr where code_mstr.code_domain = global_domain
                                 and code_mstr.code_fldname  = "workcenter_costcenter"	
                                 and entry(2,code_mstr.code_value,"-") = xxcstglctramt_det.xxcstglctramt_costcentercode
                                 no-lock no-error.
                for each xxcstctrvar_mstr where xxcstctrvar_mstr.xxcstctrvar_domain = global_domain
				                            and xxcstctrvar_mstr.xxcstctrvar_entity = xxcstglctramt_det.xxcstglctramt_entity
                                            and xxcstctrvar_mstr.xxcstctrvar_year   = xxcstglctramt_det.xxcstglctramt_year
											and xxcstctrvar_mstr.xxcstctrvar_site   = site
											and xxcstctrvar_mstr.xxcstctrvar_period = xxcstglctramt_det.xxcstglctramt_period 
											and xxcstctrvar_mstr.xxcstctrvar_wkctr  = if available code_mstr then 
											                                          entry(1,code_mstr.code_value,"-") 
																					  else
																					  xxcstglctramt_det.xxcstglctramt_costcentercode
											.
				
					assign xxcstctrvar_mstr.xxcstctrvar_lbr_allamt = xxcstglctramt_det.xxcstglctramt_lbr_varamt 
					                                                 when xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb"
                           xxcstctrvar_mstr.xxcstctrvar_bdn_allamt = xxcstglctramt_det.xxcstglctramt_bdn_varamt 
					                                                 when xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb"
					       /* will adjust to rate field */
                           xxcstctrvar_mstr.xxcstctrvar_decimal01 = if xxcstglctramt_det.xxcstglctramt_lbr_varamt
 						                                            / xxcstglctramt_det.xxcstglctramt_lbr_glabsamt = ? then 0
																	else xxcstglctramt_det.xxcstglctramt_lbr_varamt
 						                                            / xxcstglctramt_det.xxcstglctramt_lbr_glabsamt
																	when xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb"
                           xxcstctrvar_mstr.xxcstctrvar_decimal02 = if xxcstglctramt_det.xxcstglctramt_bdn_varamt
 						                                            / xxcstglctramt_det.xxcstglctramt_bdn_glabsamt = ? then 0
																	else 
																	xxcstglctramt_det.xxcstglctramt_bdn_varamt
 						                                            / xxcstglctramt_det.xxcstglctramt_bdn_glabsamt
																	when xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb"
                           xxcstctrvar_mstr.xxcstctrvar_lbr_varamt = xxcstctrvar_mstr.xxcstctrvar_decimal01 *
						                                             xxcstctrvar_mstr.xxcstctrvar_lbr_amt
																	 when xxcstglctramt_det.xxcstglctramt_glaccounttype = "labor_absorb"
                           xxcstctrvar_mstr.xxcstctrvar_bdn_varamt = xxcstctrvar_mstr.xxcstctrvar_decimal02 *
						                                             xxcstctrvar_mstr.xxcstctrvar_lbr_amt
																	 when xxcstglctramt_det.xxcstglctramt_glaccounttype = "burden_absorb"
																	.
                end. /* for each xxcstctrvar_mstr */
				
		     
			 
			 
 		     end.
			 /** end of allocate variance to part */
			 /** for overhead cost allocate ***/
			 
			 tot_comovhamt1 = 0.
			 tot_comovhamt2 = 0.
             for each xxcstglctramt_det where xxcstglctramt_det.xxcstglctramt_domain   = global_domain
	                                      and xxcstglctramt_det.xxcstglctramt_entity    = entity
	                                      and xxcstglctramt_det.xxcstglctramt_year      = li_year
	                                      and xxcstglctramt_det.xxcstglctramt_period    = li_period										  
										  and xxcstglctramt_det.xxcstglctramt_glaccounttype = "overhead_actual"
										  no-lock .
                tot_comovhamt1 = tot_comovhamt1 + xxcstglctramt_det.xxcstglctramt_ovh_glactamt.
	         end.
			
             for each xxcstctrvar_mstr where xxcstctrvar_mstr.xxcstctrvar_domain = global_domain
				                            and xxcstctrvar_mstr.xxcstctrvar_entity = entity
											and xxcstctrvar_mstr.xxcstctrvar_site   = site
                                            and xxcstctrvar_mstr.xxcstctrvar_year   = li_year
											and xxcstctrvar_mstr.xxcstctrvar_period = li_period
											no-lock .
				tot_comovhamt2 = tot_comovhamt2 + xxcstctrvar_mstr.xxcstctrvar_ovh_amt.
		     end.
			 
             for each xxcstctrvar_mstr where xxcstctrvar_mstr.xxcstctrvar_domain = global_domain
				                            and xxcstctrvar_mstr.xxcstctrvar_entity = entity
                                            and xxcstctrvar_mstr.xxcstctrvar_year   = li_year
											and xxcstctrvar_mstr.xxcstctrvar_period = li_period
											and xxcstctrvar_mstr.xxcstctrvar_site   = site
											.
											
					assign xxcstctrvar_mstr.xxcstctrvar_ovh_allamt = tot_comovhamt1  - tot_comovhamt2
					                 
                           xxcstctrvar_mstr.xxcstctrvar_decimal03 = xxcstctrvar_mstr.xxcstctrvar_ovh_allamt
 						                                            / tot_comovhamt2
																	
                           xxcstctrvar_mstr.xxcstctrvar_ovh_varamt =if  xxcstctrvar_mstr.xxcstctrvar_decimal03 *
						                                             xxcstctrvar_mstr.xxcstctrvar_ovh_amt = ?
																	 then 0
																	 else 
																	  xxcstctrvar_mstr.xxcstctrvar_decimal03 *
						                                             xxcstctrvar_mstr.xxcstctrvar_ovh_amt
																	.
              end. /* for each xxcstctrvar_mstr */
				
			 /** end of allocate variance to part for overhead cost */			 
			
	
	        
 {us/mf/mfselprt.i "printer" 132}
      for each tqApiTrialBalanceCalculation where 
            tqApiTrialBalanceCalculation.tdPeriodDebitLc  <> 0 or
			tqApiTrialBalanceCalculation.tdPeriodCreditLc <> 0 	:
			
		export tqApiTrialBalanceCalculation.	
	end.
	   for each tFilter .
	   export tFilter.
	   end.
  {us/mf/mfrtrail.i}
  
end. /* mainloop */
